<?php


namespace app\shop\controller\admin;


use app\shop\common\consts\Consts;
use app\shop\controller\BaseAdminController;
use app\shop\model\user\DepartmentModel;
use app\shop\model\user\UserModel;
use think\facade\Db;
use think\Request;

class DepartmentController extends BaseAdminController
{

    public function setModel()
    {
        // TODO: Implement setModel() method.
    }

    function setSearch()
    {
        return [
            'like' => ['name']
        ];
    }

    function setSort()
    {
        // TODO: Implement setSort() method.
    }

    /**
     * 部门列表
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function getList()
    {
        try {
            $where = [];
            $where[] = ['status', '=', 0];
            $userInfo = $this->request->middleware('userInfo');
            $roleId = $userInfo['role_id'];
            $departmentId = $userInfo['department_id'];
            if ($roleId != Consts::ADMIN_ROLE_ID) {
                $nextDepartmentId = get_child_department_id($departmentId);
                array_push($nextDepartmentId, $departmentId);
                $where[] = ['id', 'in', $nextDepartmentId];
            }
            $where = array_merge($this->searchWhere, $where);
            $list = DepartmentModel::where($where)
                ->order('id', 'desc')
                ->select()
                ->toArray();
            $list = list_to_tree($list, 'id', 'pid', 'children');
            foreach ($list as &$value) {
                $value['_showChildren'] = true;
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 添加，编辑
     * @param Request $request
     * @return \think\response\Json
     */
    public function save(array $data = [])
    {
        try {

            $id = $this->request->param('id', '', 'trim');
            if (!$id) {
                $data = $this->request->param('', '', 'trim');
                $model = new  DepartmentModel();
                $model->save($data);
            } else {
                $data = $this->request->except(['create_time']);
                $model = DepartmentModel::find($id);
                $model->save($data);
            }
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    public function del()
    {
        try {
            $id = $this->request->param('id', 0);
            $status = $this->request->param('status', 0);
            $user = UserModel::where('department_id', $id)->find();
            if ($user) {
                throw new \Exception('有用户属于该部门，不能删除');
            }
            $info = DepartmentModel::where('pid', $id)->find();
            if ($info) {
                throw new \Exception('有下属子部门，不能删除');
            }
            $model = DepartmentModel::find($id);
            $model->status = 1;
            $model->save();
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 部门列表
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function getDepartmentArr()
    {
        $userInfo = $this->request->middleware('userInfo');
        $roleId = $userInfo['role_id'];
        $departmentId = $userInfo['department_id'];
        $where = [];
        $where[] = ['status', '=', 0];
        if ($roleId != Consts::ADMIN_ROLE_ID) {
            $nextDepartmentId = get_child_department_id($departmentId);
            array_push($nextDepartmentId, $departmentId);
            $where[] = ['id', 'in', $nextDepartmentId];
        }
        $list = DepartmentModel::where($where)
            ->order('id', 'desc')
            ->select();
        return $this->success($list);
    }
}